//! Worker context traits.

use aspect_mq_proc::{MsgBatch, ProcError};
use aspect_msgqueue::QueueIdent;

use crate::ProcStateContainer;

/// Generic context to provide a processor worker with access to the underlying
/// database we operate on.
///
/// If we implement caching, we can hide it internally in this trait impl.
pub trait ProcWorkerContext {
    /// Fetches a range of messages from a specific queue on disk.
    fn fetch_message_range(
        &self,
        id: QueueIdent,
        start: u64,
        end: u64,
    ) -> Result<MsgBatch, ProcError>;

    /// Updates the last message we've read according to the queue.  This is
    /// updated *after* we've persisted the new queue state.
    fn update_read_position(&self, id: QueueIdent, last_read: u64) -> Result<(), ProcError>;

    /// Fetches the persisted queue processor state.
    fn fetch_proc_state(&self, id: QueueIdent) -> Result<ProcStateContainer, ProcError>;

    /// Stores the a new queue processor state.
    fn store_proc_state(&self, id: QueueIdent, state: ProcStateContainer) -> Result<(), ProcError>;
}
